package com.speily.current.wait_notify.deathLock;

import java.util.concurrent.locks.ReentrantLock;

/**
 * @Auther: SPL
 * @Date: 2019-07-30 14:08
 * @Description: - 多线程：售票demo
 */

public class SellTickets implements Runnable{
    private int ticketNumber = 10;
    ReentrantLock lock = new ReentrantLock();
    @Override
    public void run() {
        while(true){
            /*
            synchronized (this){
                if(ticketNumber > 0){
                    System.out.println(Thread.currentThread().getName()+"正在出售第"+ ticketNumber-- +"张票");
                }
            }
            */

            try{
                lock.lock();//上锁
                if(ticketNumber > 0){
                    System.out.println(Thread.currentThread().getName()+"正在出售第"+ ticketNumber-- +"张票");
                }
            }finally {
                lock.unlock();//解锁
            }



        }
    }
    public static void main(String[] args) {
        SellTickets seller = new SellTickets();
        Thread seller1 = new Thread(seller,"售票员1");
        Thread seller2 = new Thread(seller,"售票员2");
        Thread seller3 = new Thread(seller,"售票员3");

        seller1.start();
        seller2.start();
        seller3.start();

    }
}

